home *** CD-ROM | disk | FTP | other *** search
- /*
- * Routines to rescale 1-bit deep images by coherent scanline doubling
- * and dithered downsampling.
- *
- * Copyright (c) 1993 by Martin W. Fong
- */
-
-
- #include <malloc.h>
- #include "glassert.h"
- #include "procImage.h"
- #include "xlib.h"
-
-
- static u_char fDoSmallVideoC = False;
-
-
- void
- InitVideoC (u_char fFlag)
-
- {
- fDoSmallVideoC = fFlag;
- }
-
-
- u_char
- FDoSmallVideoC (void)
-
- {
- return fDoSmallVideoC;
- }
-
-
- u_char *
- processVideoC (u_char *ptr, u_long *pDatasize, long *pBpsl, ImageStruct *pIm)
-
- {
- if (pIm->w == 640 && pIm->h == 200 && pIm->d == 1)
- {
- u_long count;
- u_char *p ;
- int nColBytes = pIm->w >> 3;
- int nColBytes2 = nColBytes << 1;
- u_char *ptr2 = (u_char *) malloc (2L * *pDatasize);
- u_char *p2;
-
-
- assert (ptr2);
-
- /*...Complement the bits...*/
-
- for (count = *pDatasize, p = ptr;
- count /* > 0 */;
- count--, p++)
- {
- register u_char c = *p;
-
-
- *p = ~c;
- }
-
- /*...Double the image's height by interpolating consecutive scan lines...*/
-
- for (count = pIm->h, p = ptr, p2 = ptr2;
- count /* > 0 */;
- count--, p += nColBytes, p2 += nColBytes2)
- {
- if (count != 1)
- {
- u_char *p3;
- short ithByte;
-
-
- for (ithByte = 0, p3 = p2 + nColBytes;
- ithByte < nColBytes;
- ithByte++, p3++)
- {
- *p3 = *(p + ithByte) | *(p + nColBytes + ithByte);
- }
- }
-
- BlockMove ((Ptr) p, (Ptr) p2, (Size) nColBytes);
- }
-
- free ((char *) ptr);
-
- ptr = ptr2;
- pIm->h *= 2;
- *pDatasize = *pBpsl * pIm->h;
- }
-
- return ptr;
- }
-
-
- static u_char bitOnOffs[16] =
- {
- 0, /* 0 */
- 0, /* 1 */
- 0, /* 2 */
- 2, /* 3 */
- 0, /* 4 */
- 2, /* 5 */
- 2, /* 6 */
- 1, /* 7 */
- 0, /* 8 */
- 2, /* 9 */
- 2, /* 10 */
- 2, /* 11 */
- 1, /* 12 */
- 1, /* 13 */
- 1, /* 14 */
- 1 /* 15 */
- };
-
-
- u_char *
- shrinkVideoC (u_char *ptr, u_long *pDatasize, long *pBpsl, ImageStruct *pIm)
-
- {
- if (pIm->w == 640 && pIm->h == 400 && pIm->d == 1)
- {
- int nColBytes = pIm->w >> 3;
- int nColBytes2 = nColBytes << 1;
- u_char *pNew = (u_char *) malloc ((size_t) (40L * 200L));
- u_char *pNew2 = pNew;
- u_long count;
- u_char *p1;
- u_char *p2;
-
-
- assert (pNew);
-
- /*...Downsample two-to-one by examining two consecutive scan lines.
- * For each pair, use a 2 x 2 bit pattern to compute an output bit.
- * For 2 x 2 patterns with exactly two bits on, alternate between
- * off and on to effectively dither consecutive two-bits-on runs.
- */
-
- for (count = pIm->h, p1 = ptr, p2 = ptr + nColBytes;
- count /* > 0 */;
- count -=2, p1 += nColBytes2, p2 += nColBytes2)
- {
- u_char newByte = 0;
- u_char *p3;
- u_char *p4;
- short ithByte;
- u_char fLastOn = 0;
-
-
- for (ithByte = 0, p3 = p1, p4 = p2;
- ithByte < nColBytes;
- ithByte++, p3++, p4++)
- {
- u_char c1;
- u_char c2;
- u_char i;
-
-
- for (i = 0, c1 = *p3, c2 = *p4;
- i < 4;
- i++, c1 <<= 2, c2 <<= 2)
- {
- register u_char offset = ((c1 >> 4) & 0x0C) | ((c2 >> 6) & 0x03);
- register u_char bitOnOff = bitOnOffs[offset];
-
-
- if (bitOnOff & 0x02 /* != 0 */)
- {
- bitOnOff = fLastOn++;
- fLastOn &= 0x01;
- }
- else
- {
- fLastOn = 0;
- }
-
- newByte = (newByte << 1) | bitOnOff;
- }
-
- if (ithByte & 0x01 /* == 0x01 */)
- {
- *pNew2++ = newByte;
- newByte = 0;
- }
- }
- }
-
- nColBytes2 = nColBytes >> 1;
-
- for (count = pIm->h, p1 = ptr, pNew2 = pNew;
- count /* > 0 */;
- count -=2, p1 += nColBytes, pNew2 += nColBytes2)
- {
- BlockMove ((Ptr) pNew2, (Ptr) p1, (Size) nColBytes2);
- }
-
- pIm->w = 320;
- pIm->h = 200;
- *pBpsl = (pIm->w + 7) >> 3;
- *pDatasize = *pBpsl * pIm->h;
-
- free ((char *) ptr);
- ptr = pNew;
- }
-
- return ptr;
- }
-